package co.edu.unal.scrum.client.statics;

import java.util.HashMap;

import co.edu.unal.scrum.client.resources.Messages;

import com.extjs.gxt.charts.client.Chart;
import com.extjs.gxt.charts.client.model.ChartModel;
import com.extjs.gxt.charts.client.model.axis.XAxis;
import com.extjs.gxt.charts.client.model.axis.YAxis;
import com.extjs.gxt.charts.client.model.charts.BarChart;
import com.extjs.gxt.charts.client.model.charts.LineChart;
import com.extjs.gxt.charts.client.model.charts.BarChart.BarStyle;
import com.extjs.gxt.charts.client.model.charts.SketchBarChart.SketchBar;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.ViewImpl;

public class StaticsView extends ViewImpl implements StaticsPresenter.MyView {
private ContentPanel cp;
private Messages messages;
	

	@Inject
	public StaticsView(final Messages messages) {
		this.messages=messages;
	 cp = new ContentPanel();  
	 cp.setLayout(new FitLayout());
		cp.setHeading("Horizontal Bar chart");  
		String url = "ExtGWT/chart/open-flash-chart.swf";  
		final Chart chart = new Chart(url);
		chart.setChartModel(getHorizontalBarChartModel());  
		cp.add(chart);      

	}

	private ChartModel getHorizontalBarChartModel() {
		// TODO Auto-generated method stub
		 //Create a ChartModel with the Chart Title and some style attributes
		  ChartModel cm = new ChartModel("Products by month", "font-size: 14px; font-family:      Verdana; text-align: center;");
		 
		  XAxis xa = new XAxis();
		  //set the maximum, minimum and the step value for the X axis
		 String[] months = messages.months().split(",");
		 System.out.println(months[0]);
			xa.setLabels(months[0], months[1], months[2], months[3], months[4], months[5], months[6], months[7], months[8], months[9], months[10], months[11]);

		  cm.setXAxis(xa);
		  YAxis ya = new YAxis();
			ya.setRange(0, 10, 5);
			cm.setYAxis(ya);
			
			 
			BarChart sketch = new BarChart(BarStyle.THREED);
			sketch.setTooltip("#val# " + messages.products());
		
		 
			//month -> projects
			HashMap<Integer, Integer> monthProjectMap = new HashMap<Integer, Integer>();
			
			for ( int i = 0; i < months.length; i++) {
				monthProjectMap.put(i, 2*i);
			}
			
			
			
			
			for (Integer month : monthProjectMap.keySet()) {
				Integer projectCount = monthProjectMap.get(month);
				SketchBar monthBar = new SketchBar(projectCount);
				monthBar.setColour(month % 2 == 1 ? "#C5E616" : "#13A2E1");
				sketch.addBars(monthBar);
				if (projectCount > ya.getMax().intValue()) {
					ya.setMax(projectCount);
					double steps = Math.round(projectCount / 2);
					ya.setSteps(steps > 1 ? steps : 1);
				}
			}
			
			cm.addChartConfig(sketch); 
		  return cm;  	}

	@Override
	public Widget asWidget() {
		return cp;
	}

}
